var express = require('express');
var router = express.Router();

// const { getMd5 } = require('../utils/crypto')

// const bcrypt = require('bcrypt')


//登录
const { login, register } = require('../service/usersService');
const jwt = require('jsonwebtoken');

router.post('/login', async function (req, res, next) {


const data = await login(req.body)

if(data.status){
  const {username} = data.data[0]
 const token =jwt.sign(
      {username},
      'hello',
      {expiresIn:'24h'}
    )
  data.token=token
  res.send(data)
  
}else{
  res.send(data);
}

});

//注册

router.post('/register', async function (req, res, next) {

const data = await register({ usersname, password })
  res.send(data);
  
})




//geusernameinfo
router.get('/geusernameinfo', async function (req, res, next){
const aut = req.get('Authorization');
const token =aut.split(' ')[1];
const data = jwt.verify(token,'hello');
res.send({
  message:'获取用户信息成功',
  status:1,
  data:data.usersname
})
})

module.exports = router;